﻿package mus {	


    import mx.rpc.soap.WebService;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
	
	public class webServiceManager
	{
		
		public var urlWebServiceAuth:String;
		public var urlWebServiceProducts:String;
		public var urlServiceBrands:String;
		public var urlServiceLogin:String;
		
		private var wsAuth:WebService;
		private var wsProducts:WebService;
		private var wsBrands:WebService;
		private var wsLogin:WebService;
		
		private var retFunc:Function;
		
		function webServiceManager()
		{
		}
		public function init()
		{
			
            wsAuth 		= new WebService();
            wsAuth.wsdl = urlWebServiceAuth + "?wsdl";
            wsAuth.GetCustomerSessionID.addEventListener("fault", getSessionIdFault);
            wsAuth.GetCustomerSessionID.addEventListener("result", getSessionIdResult);
            wsAuth.loadWSDL();
			
			wsProducts 		= new WebService();
            wsProducts.wsdl = urlWebServiceProducts + "?wsdl";
            wsProducts.GetProduct.addEventListener("fault", getProductInfoFault);
            wsProducts.GetProduct.addEventListener("result", getProductInfoResult);
            wsProducts.loadWSDL();
			
			wsBrands 		= new WebService();
            wsBrands.wsdl 	= urlServiceBrands + "?wsdl";
            wsBrands.getProductsByBrandAndSubCatecoryID.addEventListener("fault", getProductInfoFromServiceBrandFault);
            wsBrands.getProductsByBrandAndSubCatecoryID.addEventListener("result", getProductInfoFromServiceBrandResult);
            wsBrands.loadWSDL();
			
			wsLogin 		= new WebService();
            wsLogin.wsdl 	= urlServiceLogin + "?wsdl";
            wsLogin.Login.addEventListener("fault", loginFault);
            wsLogin.Login.addEventListener("result", loginResult);
            wsLogin.loadWSDL();
		}
	
		public function set onResult(f:Function)
		{
			retFunc = f;
			
		}
		////////////////////////////////////////////////////////////////////
		// renvoi 0 si pas logué, le customer id sinon
		////////////////////////////////////////////////////////////////////
		public function getSessionId()
		{
			
			wsAuth.GetCustomerSessionID();
				
		}
        public function getSessionIdResult(event:ResultEvent):void 
        {			
			if (retFunc != null)
			{
				retFunc(event.result);
			}
        }

        public function getSessionIdFault(event:FaultEvent):void 
        {
	        trace("error webservice getSessionIdFault " + event.fault.faultString);
        }
		////////////////////////////////////////////////////////////////////
		// get produit info
		////////////////////////////////////////////////////////////////////
		public function getProductInfo(idProduct:String, subProduct:String, hairColor:String)
		{
					
			wsProducts.GetProduct(idProduct, subProduct, hairColor);

		}
        public function getProductInfoResult(event:ResultEvent):void 
        {			
			if (retFunc != null)
			{
				retFunc(event.result);
			}
        }

        public function getProductInfoFault(event:FaultEvent):void 
        {
	        trace("error webservice getProductInfoFault " + event.fault.faultString);
        }
		
		
		////////////////////////////////////////////////////////////////////
		// get produit info from serviceBrands
		////////////////////////////////////////////////////////////////////
		public function getProductInfoFromServiceBrand(subCategory:String, brand:String)
		{
			trace("getProductInfoFromServiceBrand");
			wsBrands.getProductsByBrandAndSubCatecoryID(subCategory, brand);
		}
        public function getProductInfoFromServiceBrandResult(event:ResultEvent):void 
        {			
			
			trace("getProductInfoFromServiceBrandResult " + event.result);
			if (retFunc != null)
			{
				retFunc(event.result);
			}
        }

        public function getProductInfoFromServiceBrandFault(event:FaultEvent):void 
        {
	        trace("error webservice getProductInfoFromServiceBrandFault " + event.fault.faultString);
        }
		
		////////////////////////////////////////////////////////////////////
		// login
		////////////////////////////////////////////////////////////////////
		public function login(log:String, pass:String)
		{

			wsLogin.Login(log, pass);

		}
        public function loginResult(event:ResultEvent):void 
        {			
			if (retFunc != null)
			{
				retFunc(event.result);
			}
        }

        public function loginFault(event:FaultEvent):void 
        {
	        trace("error webservice loginFault " + event.fault.faultString);
        }
		
	}
	
	
	
}